import Vue from 'vue'
import App from './App.vue'
import router from './router'
//引入ElementUI组件
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import {Upload, Button } from 'element-ui';
import moment from 'moment'
import axios from 'axios';
import VueAwesomeSwiper from 'vue-awesome-swiper';
import 'swiper/css/swiper.css'; // 引入Swiper样式

Vue.use(Upload);
Vue.use(Button);
Vue.use(VueAwesomeSwiper);
Vue.prototype.$axios = axios;
Vue.prototype.moment = moment
Vue.config.productionTip = false
Vue.use(ElementUI);

axios.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')


const axiosInstance = axios.create({
  baseURL: 'http://localhost:8082', // 设置接口的基础URL
});

// 在请求发送前设置 JWT 令牌
axiosInstance.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem('jwt'); // 从localStorage中获取JWT令牌
    if (token !=null) {
      config.headers.Token = `${token}`; // 将JWT令牌添加到请求头中
    }
    return config;
  },
  (error) => {
    this.$message.error('请先登录');
    return Promise.reject(error);
  }
);

// 响应拦截器
axiosInstance.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    return Promise.reject(error);
  }
);

// 将axios实例挂载到Vue原型上，便于全局使用
Vue.prototype.$http = axiosInstance;

